import 'dart:async';
import 'dart:io';

import 'package:process_run/shell.dart';

import '../utils/file_util.dart';
import '../val/args_val.dart';
import 'basic_shell.dart';

class FileCommand extends BasicShell {
  var shell = Shell(runInShell: true);

  FileCommand(super.args);

  @override
  Future doExecute() async {
    // 定义要进入的目录和要执行的脚本
    String argPath = args[ArgsVal.project];

    String fileType = args[ArgsVal.fileType];
    String path = Directory.current.path;

    String sourceDir = "$path/sh";
    String sourceSh = "$path/sh/self.sh";

    String logType = "$path/sh/log$fileType";

    print("创建文件$sourceDir");
    if (!Directory(sourceDir).existsSync()) {
      Directory(sourceDir).createSync();
    }

    print("创建文件$sourceSh");
    await FileUtil.writeText(sourceSh, """#!/bin/sh \n # 记录一下开始时间 \necho "//"+ `date` >> log$fileType \necho '//finish' >> log$fileType""");
    print("创建文件$logType");
    await FileUtil.writeText(logType, "");

    String destDirectory = argPath;
    String destDir = "$destDirectory/sh";
    if (!Directory(destDir).existsSync()) {
      Directory(destDir).createSync();
    }
    print("创建文件$destDir");
    await FileUtil.copyDirectory(sourceDir, destDir);
    print("创建文件$logType");
    print(path);
  }
}
